<?php namespace Admin\Controller;
use Admin\Model\Node;
use Admin\Model\Access;
//权限管理控制器
class AuthRuleController extends CommonController{
	protected $model;
	//构造函数实例化节点表模型
	public function __auto(){
		$this->model=new Node();
	}

	//节点列表
	public function index(){
		$data=Cache::get('allRole');
		if (!$data) {
			$data=$this->model->orderBy('sort','ASC')->get();
			$data=$this->model->getTree($data);
			Cache::set('allRole',$data,0);
		}
		View::with('data',$data)->make();
	}

	//节点编辑
	public function edit(){
		if (IS_POST) {
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('修改成功','index');
		}
		$id=Q('get.id',0,'intval');
		$data=$this->model->where("id={$id}")->find();
		$tree=$this->model->getTree($this->model->getNoMy($id));
		View::with('tree',$tree);
		View::with('data',$data)->make();
	}

	//添加节点
	public function add(){
		if (IS_POST) {
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功','index');
		}
		//获得所有的节点的树状结构
		$tree=$this->model->getTree();
		View::with('tree',$tree)->make();
	}

	//删除节点
	public function del(){
		$id=Q('get.id',0,'intval');
		if ($this->model->getSon($id)) View::error('请先删除子规则','index');
		$accessModel=new Access();
		$accessModel->where("node_id={$id}")->delete();
		$this->model->where("id={$id}")->delete();
		View::success('删除成功','index');
	}

	//排序选中节点
	public function beachdelete(){
		$this->model->reSort();
		View::success('排序完成','index');
	}

	//更新缓存
	public function updateCache(){
		Cache::del('allRole');
		View::success('已清除缓存','index');
	}

}